package filters;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import beanUtils.BeanUtility;
import beans.PersonalDetailsBean;

/**
 * Servlet Filter implementation class personaldetails_filter
 */
public class personaldetails_filter implements Filter {
	int count = 0;

	/**
	 * Default constructor.
	 */
	public personaldetails_filter() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		System.out.println("in pd filter");
		System.out.println("in pd filter");
		Map<String, String> errormap = new HashMap<String, String>();
		PersonalDetailsBean pdb = new PersonalDetailsBean();
		BeanUtility.populateBean(pdb, (HttpServletRequest) request);
		System.out.println(pdb.toString());
		try {
			validate(pdb, errormap);
			if (count > 0) {
				count = 0;
				request.setAttribute("errormap", errormap);
				RequestDispatcher rd = request
						.getRequestDispatcher("registration.jsp");
				rd.forward(request, response);
			}
		} catch (Exception e) {
			// e.printStackTrace();
		}

		// pass the request along the filter chain
		chain.doFilter(request, response);
	}

	private void validate(PersonalDetailsBean pdb, Map<String, String> errormap)
			throws Exception {
		System.out.println("in pd validate function");
		fname(pdb.getFname(), errormap);
		lname(pdb.getLname(), errormap);
		dob();
		mobileno(pdb.getMobileno(), errormap);
		email(pdb.getEmail(), errormap);
		panno(pdb.getPanno(), errormap);
		city();
		cpincode(pdb.getC_pincode(), errormap);
		city();
		opincode(pdb.getO_pincode(), errormap);
		teloffice(pdb.getOfficeno(), errormap);
		telhome(pdb.getHomeno(), errormap);
		fax(pdb.getFaxno(), errormap);

	}

	private void fax(String fax, Map<String, String> errormap) {
		if (fax.equals("")) {
			errormap.put("fax", "enter fax no");
			count++;
		} else {
			String regex = "^\\+?[0-9]{7,}$";
			Pattern p = Pattern.compile(regex);
			Matcher match = p.matcher(fax.trim());
			boolean matchfound = match.matches();
			// boolean iserrorfound=false;
			if (!matchfound) {
				System.out.println("In fax function : " + fax);
				errormap.put("fax", "invalid number");
				// iserrorfound=true;
				count++;
				System.out.println("******" + count);
			}
		}
	}

	private void teloffice(String officeno, Map<String, String> errormap) {
		if (officeno.equals("")) {
			errormap.put("officeno", "enter number");
			count++;
		} else {

			String regex = "^[0-9]\\d{2,4}-\\d{6,8}$";
			Pattern p = Pattern.compile(regex);
			Matcher match = p.matcher(officeno.trim());
			boolean matchfound = match.matches();
			// boolean iserrorfound=false;
			if (!matchfound) {
				System.out.println("In officeno function : " + officeno);
				errormap.put("officeno", "invalid number");
				// iserrorfound=true;
				count++;
				System.out.println("******" + count);
			}
		}

	}

	private void telhome(String homeno, Map<String, String> errormap) {
		if (homeno.equals("")) {
			errormap.put("homeno", "enter number");
			count++;
		} else {
			String regex = "\\d{3}-\\d{7}";
			Pattern p = Pattern.compile(regex);
			Matcher match = p.matcher(homeno.trim());
			boolean matchfound = match.matches();
			// boolean iserrorfound=false;
			if (!matchfound) {
				System.out.println("In homeno function : " + homeno);
				errormap.put("homeno", "invalid number");
				// iserrorfound=true;
				count++;
				System.out.println("******" + count);
			}
		}
	}

	private void cpincode(String cpincode, Map<String, String> errormap) {
		if (cpincode.equals("")) {
			errormap.put("cpincode", "enter pincode");
			count++;

		} else {

			String regex = "^[0-9]{6}";
			Pattern p = Pattern.compile(regex);
			Matcher match = p.matcher(cpincode.trim());
			boolean matchfound = match.matches();
			// boolean iserrorfound=false;
			if (!matchfound) {
				System.out.println("In cpincode function : " + cpincode);
				errormap.put("cpincode", "invalid pincode");
				// iserrorfound=true;
				count++;
				System.out.println("******" + count);
			}
		}

	}

	private void opincode(String opincode, Map<String, String> errormap) {
		if (opincode.equals("")) {
			errormap.put("opincode", "enter pincode");
			count++;

		} else {

			String regex = "^[0-9]{6}";
			Pattern p = Pattern.compile(regex);
			Matcher match = p.matcher(opincode.trim());
			boolean matchfound = match.matches();
			// boolean iserrorfound=false;
			if (!matchfound) {
				System.out.println("In opincode function : " + opincode);
				errormap.put("opincode", "invalid pincode");
				// iserrorfound=true;
				count++;
				System.out.println("******" + count);
			}
		}

	}

	private void city() {
		// TODO Auto-generated method stub

	}

	private void panno(String panno, Map<String, String> errormap) {
		if (panno.equals("")) {
			errormap.put("panno", "enter pan number");
			count++;

		} else {

			String regex = "[A-Za-z]{5}\\d{4}[A-Za-z]{1}";
			Pattern p = Pattern.compile(regex);
			Matcher match = p.matcher(panno.trim());
			boolean matchfound = match.matches();
			// boolean iserrorfound=false;
			if (!matchfound) {
				System.out.println("In panno function : " + panno);
				errormap.put("panno", "invalid pan number");
				// iserrorfound=true;
				count++;
				System.out.println("******" + count);
			}
		}
	}

	private void email(String email, Map<String, String> errormap) {
		if (email.equals("")) {
			errormap.put("email", "enter email id");
			count++;

		} else {
			String regex = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
					+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
			Pattern p = Pattern.compile(regex);
			Matcher match = p.matcher(email.trim());
			boolean matchfound = match.matches();
			// boolean iserrorfound=false;
			if (!matchfound) {
				System.out.println("In email id function : " + email);
				errormap.put("email", "invalid email id");
				// iserrorfound=true;
				count++;
				System.out.println("******" + count);
			}
		}
	}

	private void mobileno(String mobile, Map<String, String> errormap) {
		if (mobile.equals("")) {
			errormap.put("mobile", "enter mobile number");
			count++;

		} else {
			String regex = "[0-9]{10}";
			Pattern p = Pattern.compile(regex);
			Matcher match = p.matcher(mobile.trim());
			boolean matchfound = match.matches();
			// boolean iserrorfound=false;
			if (!matchfound) {
				System.out.println("In mobile function : " + mobile);
				errormap.put("mobile", "invalid mobile number");
				// iserrorfound=true;
				count++;
				System.out.println("******" + count);
			}
		}
	}

	private void dob() {
		// TODO Auto-generated method stub

	}

	private void fname(String name, Map<String, String> errormap) {
		if (name.equals("")) {
			errormap.put("fname", "enter first name");
			count++;

		} else {
			String regex = "[a-zA-Z][a-zA-Z]*";
			Pattern p = Pattern.compile(regex);
			Matcher match = p.matcher(name.trim());
			boolean matchfound = match.matches();
			// boolean iserrorfound=false;
			if (!matchfound) {
				System.out.println("In fname function : " + name);
				errormap.put("fname", "invalid first name");
				// iserrorfound=true;
				count++;
				System.out.println("******" + count);
			}
		}
	}

	private void lname(String name, Map<String, String> errormap) {
		if (name.equals("")) {
			errormap.put("lname", "enter last name");
			count++;

		} else {
			String regex = "[a-zA-Z][a-zA-Z]*";
			Pattern p = Pattern.compile(regex);
			Matcher match = p.matcher(name.trim());
			boolean matchfound = match.matches();
			// boolean iserrorfound=false;
			if (!matchfound) {
				System.out.println("In lname function : " + name);
				errormap.put("lname", "invalid last name");
				// iserrorfound=true;
				count++;
				System.out.println("******" + count);
			}
		}
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
